﻿using System.Data;
using System.Data.Objects;
using System.Data.SqlClient;
using System.Linq;
using Books.Data.ModelProxies;
using Books.Model;

namespace Books.Data
{
    internal class BooksContext : ObjectContext
    {
        public BooksContext()
        : base("name=BooksConnection")
        {
        }

        public AuthorModel GetAuthorById(int authorId)
        {
            return ExecuteStoreQuery<AuthorProxy>("GetAuthorById @authorId",
                new SqlParameter() { ParameterName = "authorId", SqlDbType = SqlDbType.Int, Value = authorId }).FirstOrDefault();
        }

        public AuthorModel[] GetAuthors()
        {
            return ExecuteStoreQuery<AuthorProxy>("GetAuthors").ToArray();
        }

        public AuthorModel[] GetAuthorsForBook(int bookId)
        {
            return ExecuteStoreQuery<AuthorProxy>("GetAuthorsForBook @bookId",
                new SqlParameter() { ParameterName = "bookId", SqlDbType = SqlDbType.Int, Value = bookId }).ToArray();
        }

        public AuthorModel[] GetAuthorsForPublisher(int publisherId)
        {
            return ExecuteStoreQuery<AuthorProxy>("GetAuthorsForPublisher @publisherId",
                new SqlParameter() { ParameterName = "publisherId", SqlDbType = SqlDbType.Int, Value = publisherId }).ToArray();
        }

        public BookModel GetBookById(int bookId)
        {
            return ExecuteStoreQuery<BookProxy>("GetBooksById @bookId",
                new SqlParameter() { ParameterName = "bookId", SqlDbType = SqlDbType.Int, Value = bookId }).FirstOrDefault();
        }

        public BookModel[] GetBooks()
        {
            return ExecuteStoreQuery<BookProxy>("GetBooks").ToArray();
        }

        public BookModel[] GetBooksForAuthor(int authorId)
        {
            return ExecuteStoreQuery<BookProxy>("GetBooksForAuthor @authorId",
                new SqlParameter() { ParameterName = "authorId", SqlDbType = SqlDbType.Int, Value = authorId }).ToArray();
        }

        public BookModel[] GetBooksForPublisher(int publisherId)
        {
            return ExecuteStoreQuery<BookProxy>("GetBooksForPublisher @publisherId",
                new SqlParameter() { ParameterName = "publisherId", SqlDbType = SqlDbType.Int, Value = publisherId }).ToArray();
        }

        public PublisherModel GetPublisherById(int publisherId)
        {
            return ExecuteStoreQuery<PublisherProxy>("GetPublisherById @publisherId",
                new SqlParameter() { ParameterName = "publisherId", SqlDbType = SqlDbType.Int, Value = publisherId }).FirstOrDefault();
        }

        public PublisherModel[] GetPublishers()
        {
            return ExecuteStoreQuery<PublisherProxy>("GetPublishers").ToArray();
        }

        public PublisherModel[] GetPublishersForAuthor(int authorId)
        {
            return ExecuteStoreQuery<PublisherProxy>("GetPublishersForAuthor @authorId",
                new SqlParameter() { ParameterName = "authorId", SqlDbType = SqlDbType.Int, Value = authorId }).ToArray();
        }
    }
}
